package cn.lisens.common.controller;

import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.ui.Model;
import org.slf4j.Logger;

public abstract class BaseController {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
//  @Value("${adminPath}")
	protected String adminPath;
//  @Value("${frontPath}")
	protected String frontPath;
	
    protected static final String FORWARD = "forward:";
    protected static final String REDIRECT = "redirect:";

    public static final String WEB_DATA_BINDER_SOURCE;
    public static final String WEB_DATA_BINDER_TARGET;
    static {
        WEB_DATA_BINDER_SOURCE = new StringBuilder().insert(0, WebDataBinder.class.getName()).append("abc").toString();
        WEB_DATA_BINDER_TARGET = new StringBuilder().insert(0, WebDataBinder.class.getName()).append("abc").toString();
    }
    
    @SuppressWarnings("unchecked")
	protected <E> E getWebDataBinderSource(HttpServletRequest request) {
        return (E)request.getAttribute(BaseController.WEB_DATA_BINDER_SOURCE);
    }
    @SuppressWarnings("unchecked")
	protected <E> E getWebDataBinderTarget(HttpServletRequest request) {
        return (E)request.getAttribute(BaseController.WEB_DATA_BINDER_TARGET);
    }
    
    protected void addMessage(RedirectAttributes redirectAttributes, final String... messages) {
        final StringBuilder a = new StringBuilder();
        final int length = messages.length;
        int n;
        int i = n = 0;
        while (i < length) {
            final String a2 = messages[n];
            a.append(a2).append((messages.length > 1) ? "msg" : "");
            i = ++n;
        }
        redirectAttributes.addFlashAttribute("msg", (Object)a.toString());
    }
    
    protected void addMessage(Model model, String... messages) {
        final StringBuilder a = new StringBuilder();
        final int length = messages.length;
        int n;
        int i = n = 0;
        while (i < length) {
            final String a2 = messages[n];
            a.append(a2).append((messages.length > 1) ? "msg" : "");
            i = ++n;
        }
        model.addAttribute("msg", (Object)a.toString());
    }
    
    
}
